package com.itheima.hibernate.demo1;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import com.itheima.hibernate.domain.Customer;
import com.itheima.hibernate.utils.HibernateUtils;

/**
 * 测试当前线程绑定的Session
 * @author unicorn
 * @version v1.0
 * @createTime 2019年2月23日 下午6:13:00
 */
public class HibernateDemo3 {

	@Test
	// 一级缓存的清空
	public void demo1() {
		Session session = HibernateUtils.openSession();
		Transaction tx = session.beginTransaction();
		
		Customer customer1 = session.get(Customer.class, 1l); // 发送SQL语句查询，同时放入到一级缓存中
		
		/*session.clear(); // 清空所有
		session.evict(customer1);*/
		
		Customer customer2 = session.get(Customer.class, 1l); // 发送SQL语句查询
		System.out.println(customer1);
		System.out.println(customer2);
		
		
		tx.commit();
		session.close();
	}
	
	@Test
	public void demo2() {
		Session session = HibernateUtils.getCurrentSession();
		Transaction tx = session.beginTransaction();
		
		Customer customer = new Customer();
		customer.setCust_name("如梦");
		
		session.save(customer);
		
		tx.commit();
		// session.close(); //线程一结束session自动关闭,不需要再关闭了
	}
}
